//
//  MOBadgeView.m
//  01,我的微博 (环境配置)
//
//  Created by user on 16/3/5.
//  Copyright © 2016年 son. All rights reserved.
//

#define MOBadgeViewFont  [UIFont systemFontOfSize:11]

#import "MOBadgeView.h"

@implementation MOBadgeView


//init方法
- (instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame:frame]) {
        
        self.userInteractionEnabled = NO;
        
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        
        //设置字体的大小
        self.titleLabel.font = MOBadgeViewFont;
        
        
        //图片的大小就是按钮的大小
        [self sizeToFit];
    }
    return self;
}

//重写属性的set方法
- (void)setBadgeValue:(NSString *)badgeValue{
    _badgeValue = badgeValue;
    
    // 判断badgeValue是否有内容
    if (badgeValue.length == 0 || [badgeValue isEqualToString:@"0"]) { // 没有内容或者空字符串,等于0
        self.hidden = YES;
    }else{
        self.hidden = NO;
    }
    
    //文字的尺寸
    CGSize size = [badgeValue sizeWithFont:MOBadgeViewFont];
    
    if(size.width > self.frame.size.width){
        //文字的尺寸大于控件的宽度
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        [self setTitle:nil forState:UIControlStateNormal];
        [self setBackgroundImage:nil forState:UIControlStateNormal];
    } else{
        [self setBackgroundImage:[UIImage imageNamed:@"main_badge"] forState:UIControlStateNormal];
        [self setTitle:badgeValue forState:UIControlStateNormal];
        [self setImage:nil forState:UIControlStateNormal];
    }
    
}

@end
